package bb.lanxing.lib.devices.core.sync;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;

import com.tencent.connect.common.Constants;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import bb.lanxing.R;
import bb.lanxing.lib.devices.core.utils.SyncSharedPreference;

public abstract class SyncService extends Service {
    static SyncService instance;
    String CHANNEL_ID_SYNC_SERVICE = Constants.VIA_SHARE_TYPE_MINI_PROGRAM;
    final Map<String, SyncManagerHolder> syncManagerHolderMap = new HashMap();

    protected abstract SyncManagerFactory getSyncManagerFactory();

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        if (!SyncSharedPreference.getSyncMode(this)) {
            return Service.START_NOT_STICKY;
        }
        showNotification();
        return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new SyncManagerFactoryBinder();
    }

    @SuppressLint("ForegroundServiceType")
    private void showNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), this.CHANNEL_ID_SYNC_SERVICE);
        builder.setSmallIcon(R.drawable.ic_notification).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).setOngoing(true).setUsesChronometer(true).setWhen(System.currentTimeMillis()).setContentTitle("蓝行蓝牙传输服务").setContentText("服务启动中...").setTicker("蓝行骑行").setDefaults(-1).setAutoCancel(false).setLocalOnly(true).setChannelId(this.CHANNEL_ID_SYNC_SERVICE).setPriority(1).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

        NotificationChannel notificationChannel = new NotificationChannel(this.CHANNEL_ID_SYNC_SERVICE, "蓝行骑行", NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setDescription("蓝行蓝牙传输服务");
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
        Notification build = builder.build();
        builder.setPublicVersion(build);
        startForeground(9999, build);
        notificationManager.notify(9999, build);
    }

    public static void removeSyncManager(String str, String str2) {
        SyncService syncService = instance;
        if (syncService == null) {
            return;
        }
        SyncManagerHolder syncManagerHolder = syncService.syncManagerHolderMap.get(str2);
        if (!syncManagerHolder.hasSyncManager(str)) {
            return;
        }
        syncManagerHolder.removeSyncManager(str);
    }

    public SyncManager createSyncManager(String str, String str2, Bundle bundle) {
        SyncManager create;
        SyncManagerFactory syncManagerFactory = getSyncManagerFactory();
        SyncManagerHolder syncManagerHolder = this.syncManagerHolderMap.get(str2);
        if (syncManagerHolder == null) {
            SyncManagerHolder syncManagerHolder2 = new SyncManagerHolder();
            create = syncManagerFactory.create(str, str2, bundle);
            syncManagerHolder2.addSyncManager(create);
            this.syncManagerHolderMap.put(str2, syncManagerHolder2);
        } else if (syncManagerHolder.hasSyncManager(str)) {
            create = syncManagerHolder.getSyncManger(str);
        } else {
            create = syncManagerFactory.create(str, str2, bundle);
            syncManagerHolder.addSyncManager(create);
        }
        return new SyncManagerImpl(str2, create);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        instance = null;
        for (SyncManagerHolder syncManagerHolder : this.syncManagerHolderMap.values()) {
            Iterator<SyncManager> it = syncManagerHolder.iterator();
            while (it.hasNext()) {
                it.next().release();
            }
        }
        this.syncManagerHolderMap.clear();
    }


    private class SyncManagerFactoryBinder extends Binder implements SyncManagerFactory {
        public SyncManagerFactoryBinder() {
        }

        @Override
        public SyncManager create(String str, String str2, Bundle bundle) {
            return SyncService.this.createSyncManager(str, str2, bundle);
        }
    }

    public static class SyncManagerImpl extends Binder implements SyncManager {
        private String address;
        private SyncManager syncManager;

        public SyncManagerImpl(String str, SyncManager syncManager) {
            this.address = str;
            this.syncManager = syncManager;
        }

        @Override
        public void readFileList() {
            this.syncManager.readFileList();
        }

        @Override
        public void release() {
            SyncService.removeSyncManager(this.syncManager.getClass().getName(), this.address);
            this.syncManager.release();
            this.syncManager = null;
        }

        @Override
        public boolean sync(DeviceFile deviceFile) {
            return this.syncManager.sync(deviceFile);
        }

        @Override
        public boolean sync() {
            return this.syncManager.sync();
        }

        @Override
        public boolean sync(String str) {
            return this.syncManager.sync(str);
        }

        @Override
        public boolean sync(long j) {
            return this.syncManager.sync(j);
        }

        @Override
        public boolean resync(DeviceFile deviceFile) {
            return this.syncManager.resync(deviceFile);
        }

        @Override
        public String getPath(long j) {
            return this.syncManager.getPath(j);
        }

        @Override
        public void abort() {
            this.syncManager.abort();
        }

        @Override
        public void abortAll() {
            this.syncManager.abortAll();
        }

        @Override
        public boolean isSynchronised(long j) {
            return this.syncManager.isSynchronised(j);
        }

        @Override
        public boolean isSynchronising() {
            return this.syncManager.isSynchronising();
        }

        @Override
        public int getSyncState(long j) {
            return this.syncManager.getSyncState(j);
        }

        @Override
        public void registerSyncListener(DeviceSyncListener deviceSyncListener) {
            this.syncManager.registerSyncListener(deviceSyncListener);
        }

        @Override
        public void unregisterSyncListener(DeviceSyncListener deviceSyncListener) {
            this.syncManager.unregisterSyncListener(deviceSyncListener);
        }

        @Override
        public boolean isSynchronisingWithMultiFiles() {
            return this.syncManager.isSynchronisingWithMultiFiles();
        }

        @Override
        public void delete(long j) {
            this.syncManager.delete(j);
        }

        @Override
        public void deleteAll() {
            this.syncManager.deleteAll();
        }

        @Override
        public Object getTag() {
            return this.syncManager.getTag();
        }

        @Override
        public void setTag(Object obj) {
            this.syncManager.setTag(obj);
        }
    }
}